﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class UserControl_Role_updateQuyenLevel : System.Web.UI.UserControl
{
#region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DAQuyen objQuyen;
    DALevel objLevel;
    DAChucNang objChucNang;
    DANguoidung objNguoidung;
    DALog objLog;
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
#endregion
#region "Private methods"
    // Hien thi Quyen tren datagrid
    private void loadLevel()
    {
        DataSet dsLevel = objLevel.LayDanhSachLevel();
        BindData.bindListBox(this.uxListLevelBoxLeft, dsLevel, "ID", "Ten");
    }
    // Hien thi Quyen tren datagrid
    private void loadChucNang()
    {
        DataSet dsChucNang = objChucNang.LayDanhSachChucNang();
        BindData.bindListBox(this.uxListChucNangBoxLeft, dsChucNang, "ID", "TenChucNang");
    }
    private void loadNhomID(DropDownList ddlTemp)
    {
        DataSet dsQuyen = objQuyen.loadQuyen();
        if (dsQuyen != null && dsQuyen.Tables[0].Rows.Count > 0)
        {
            DataRow[] drs = dsQuyen.Tables[0].Select("NhomID = 0");
            ddlTemp.Items.Clear();
            foreach (DataRow dr in drs)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = " " + dr["Tennhom"].ToString();
                liTemp.Value = dr["ID"].ToString();
                ddlTemp.Items.Add(liTemp);
                loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dsQuyen.Tables[0], " ---");
            }
            ddlTemp.Visible = true;
        }
        else
        {
            ddlTemp.Visible = false;
        }
    }
    private void loadNhomIDSub(DropDownList ddlTemp, string strCheck, DataTable dt, string strPath)
    {
        DataRow[] drs = dt.Select("NhomID =" + strCheck);
        foreach (DataRow dr in drs)
        {
            ListItem liTemp = new ListItem();
            liTemp.Text = strPath + dr["Tennhom"].ToString();
            liTemp.Value = dr["ID"].ToString();
            ddlTemp.Items.Add(liTemp);
            loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dt, strPath + "---");
        }
    }
    //Delete QuyenLevel
    private void DeleteQuyenLevel()
    {
        int intQuyenID = Convert.ToInt32(this.ddlNhomID.SelectedValue);
        int intCheck = objQuyen.removeQuyenLevel(intQuyenID);
        if (intCheck > 0)
        {
            this.lblMess.Text = "";
        }
       
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
        }
    }
    //Delete Quyen Chuc Nang
    private void DeleteQuyenChucNang()
    {
        int intQuyenID = Convert.ToInt32(this.ddlNhomID.SelectedValue);
        int intCheck = objQuyen.removeQuyenChucNang(intQuyenID);
        if (intCheck > 0)
        {
            this.lblMess.Text = "";
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
        }
    }
#endregion
#region "Events handle"
    private void CheckUserModule(int UserID, int ModuleID)
    {
        
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objQuyen = new DAQuyen(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objChucNang = new DAChucNang(sqlConnec);
        objLog = new DALog(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        intModuleID = Convert.ToInt32(Request.QueryString["ChucNang"].Trim());
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            CheckUserModule(intUserID, intModuleID);
            loadNhomID(this.ddlNhomID);
            loadLevel();
            loadChucNang();
        }
    }
    //Level
    protected void uxLevelRightBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (uxListLevelBoxLeft.Items.Count > 0 && uxListLevelBoxLeft.SelectedItem != null)
        {
             ListItem selectedItem = uxListLevelBoxLeft.SelectedItem;
             selectedItem.Selected = false;
             uxListLevelBoxRight.Items.Add(selectedItem);
             uxListLevelBoxLeft.Items.Remove(selectedItem);
        }
    }
    protected void uxLevelRightAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.uxListLevelBoxLeft.Items)
        {
            this.uxListLevelBoxRight.Items.Add(ltTemp);
        }
        this.uxListLevelBoxLeft.Items.Clear();
    }
    protected void uxLevelLeftBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (uxListLevelBoxRight.Items.Count > 0 && uxListLevelBoxRight.SelectedItem != null)
        {
            ListItem selectedItem = uxListLevelBoxRight.SelectedItem;
            selectedItem.Selected = false;
            uxListLevelBoxLeft.Items.Add(selectedItem);
            uxListLevelBoxRight.Items.Remove(selectedItem);
        }
    }
    protected void uxLevelLeftAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.uxListLevelBoxRight.Items)
        {
            this.uxListLevelBoxLeft.Items.Add(ltTemp);
        }
        this.uxListLevelBoxRight.Items.Clear();
    }
    //Chucnang
    protected void uxChucNangRightBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (uxListChucNangBoxLeft.Items.Count > 0 && uxListChucNangBoxLeft.SelectedItem != null)
        {
            ListItem selectedItem = uxListChucNangBoxLeft.SelectedItem;
            selectedItem.Selected = false;
            uxListChucNangBoxRight.Items.Add(selectedItem);
            uxListChucNangBoxLeft.Items.Remove(selectedItem);
        }
    }
    protected void uxChucNangRightAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.uxListChucNangBoxLeft.Items)
        {
            this.uxListChucNangBoxRight.Items.Add(ltTemp);
        }
        this.uxListChucNangBoxLeft.Items.Clear();
    }
    protected void uxChucNangLeftBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (uxListChucNangBoxRight.Items.Count > 0 && uxListChucNangBoxRight.SelectedItem != null)
        {
            ListItem selectedItem = uxListChucNangBoxRight.SelectedItem;
            selectedItem.Selected = false;
            uxListChucNangBoxLeft.Items.Add(selectedItem);
            uxListChucNangBoxRight.Items.Remove(selectedItem);
        }
    }
    protected void uxChucNangLeftAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.uxListChucNangBoxRight.Items)
        {
            this.uxListChucNangBoxLeft.Items.Add(ltTemp);
        }
        this.uxListChucNangBoxRight.Items.Clear();
    }
    protected void ddlNhomID_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadLevel();
        loadChucNang();
        this.uxListLevelBoxRight.Items.Clear();
        this.uxListChucNangBoxRight.Items.Clear();
        int intQuyenID = Convert.ToInt32(this.ddlNhomID.SelectedValue);
        //Level
        DataSet dsLevelView = objQuyen.loadQuyenLevel(intQuyenID);
        foreach (DataRow dr in dsLevelView.Tables[0].Rows)
        {
            for (int i = 0; i < this.uxListLevelBoxLeft.Items.Count; i++)
            {
                if (dr["ID"].ToString() == this.uxListLevelBoxLeft.Items[i].Value)
                {
                    this.uxListLevelBoxLeft.Items.RemoveAt(i);
                    break;
                }
            }
            bool check = true;
            for (int i = 0; i < this.uxListLevelBoxRight.Items.Count; i++)
            {
                if (dr["ID"].ToString() == this.uxListLevelBoxRight.Items[i].Value)
                {
                    check = false;
                    break;
                }
            }
            if (check)
            {
                this.uxListLevelBoxRight.Items.Add(new ListItem(dr["Ten"].ToString(), dr["ID"].ToString()));
            }
        }
        //ChưcNang
        DataSet dsChucNangView = objQuyen.loadQuyenChucnang(intQuyenID);
        foreach (DataRow dr in dsChucNangView.Tables[0].Rows)
        {
            for (int i = 0; i < this.uxListChucNangBoxLeft.Items.Count; i++)
            {
                if (dr["ID"].ToString() == this.uxListChucNangBoxLeft.Items[i].Value)
                {
                    this.uxListChucNangBoxLeft.Items.RemoveAt(i);
                    break;
                }
            }
            bool check = true;
            for (int i = 0; i < this.uxListChucNangBoxRight.Items.Count; i++)
            {
                if (dr["ID"].ToString() == this.uxListChucNangBoxRight.Items[i].Value)
                {
                    check = false;
                    break;
                }
            }
            if (check)
            {
                this.uxListChucNangBoxRight.Items.Add(new ListItem(dr["TenChucNang"].ToString(), dr["ID"].ToString()));
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
    
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DeleteQuyenLevel();
            int intQuyenID = Convert.ToInt32(this.ddlNhomID.SelectedValue);
            int intLevel = 0;
            //Level
            for(int i = 0; i < uxListLevelBoxRight.Items.Count; i++)
            {
                intLevel = Convert.ToInt32(uxListLevelBoxRight.Items[i].Value);
                int intChẹck = objQuyen.InsertQuyenLevel(intLevel, intQuyenID, boolRead, boolWrite, boolUpdate, boolDelete);
                if (intChẹck > 0)
                {
                    this.lblMess.Text = "Cập nhật thành công bản ghi";
                    Session["UserID"] = intUserID;
                    objLog.insertLog(0, 0, 7, intUserID);
                }
                else if (intChẹck == 0)
                {
                    this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                }
                else
                {
                    this.lblMess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
                }
            }
            int intChucNang = 0;
            DeleteQuyenChucNang();
            for (int j = 0; j < uxListChucNangBoxRight.Items.Count; j++)
            {
                intChucNang = Convert.ToInt32(uxListChucNangBoxRight.Items[j].Value);
                int intChẹck = objQuyen.InsertQuyenChucNang(intQuyenID, intChucNang);
                if (intChẹck > 0)
                {
                    this.lblMess.Text = "Cập nhật thành công bản ghi";
                }
                else if (intChẹck == 0)
                {
                    this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                }
                else
                {
                    this.lblMess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
                }
            }
        }
    }
#endregion
}
